package nccloud.arap.guarantee.guaranteecostvo.action;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;

import org.owasp.esapi.ESAPI;

import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.io.WebFile;
import nccloud.framework.core.io.WebFileDisposition;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.itf.arap.manual.aggmanualcostvo.IManualcostvoService;

public class ExportTempletAction implements ICommonAction {

	@Override
	public Object doAction(IRequest request) {
		
		WebFile webFile = null;
		try {
			Map<String, String[]> webMap = request.readParameters();
			
			File file =ServiceLocator.find(IManualcostvoService.class).getExcelFile(webMap.get("colunmNames"));
			webFile = new WebFile(webMap.get("fileNames")[0]+".xls", file.length(), new FileInputStream(file));
			
		} catch (Exception e) {
			e.printStackTrace();
			Object ex = new RuntimeException(e);
			Throwable cause = ExceptionUtils.unmarsh((Throwable)ex);
			String mess = ESAPI.encoder().encodeForHTML(cause.getMessage());
			InputStream in = new ByteArrayInputStream(mess.getBytes());
			webFile = new WebFile("print_error.html", in);
		    webFile.setDisposition(WebFileDisposition.Inline);
		}
		return webFile;
	}

}
